package com.example.dllo.my36kr.tools;

import android.os.Handler;
import android.os.Looper;

import com.example.dllo.my36kr.my.collect.CollectBean;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.QueryListener;


/**
 * Created by dllo on 16/8/31.
 */
public class DBTool {
    //liteOrm 对象,曾删改查方法 都操作此对象
    private LiteOrm mLiteOrm;
    //Handler 用来做线程切换,为了在主线程来执行回调,返回数据
    private Handler mHandler;

    //线程池  核心线程数Runtime.getRuntime().availableProcessors() + 1
    ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);


    private static DBTool ourInstance = new DBTool();

    public static DBTool getInstance() {
        return ourInstance;
    }


    private DBTool() {
        //初始化liteOrm对象
        //使用MyApp里的全局Context
        //第二个参数是 数据库的名字
        mLiteOrm = LiteOrm.newSingleInstance(MyApp.getContext(), "MyDataBase.db");
        //初始化Handler
        //Handler的构造方法 参数如果传
        // Looper.getMainLooper()
        // 代表这个Handler 一定会回调到主线程
        // 属于严谨的写法

        mHandler = new Handler(Looper.getMainLooper());

    }

    public void insertUser(final CollectBean collectBean) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
//                if (!hasCollectBean(collectBean)) {
                    mLiteOrm.insert(collectBean);
                }
//            }
        });

    }

    //去重复
//    private boolean hasCollectBean(CollectBean collectBean) {
//        ArrayList<CollectBean> query = mLiteOrm.query(CollectBean.class);
//        for (CollectBean bean : query) {
//            if (bean.getUrl().equals(collectBean.getUrl())) {
//                return true;
//            }
//        }
//        return false;
//    }
    //查询用户名的方法

    public void queryUesr(final QueryListener queryListener) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                ArrayList<CollectBean> collectBean = mLiteOrm.query(CollectBean.class);
                mHandler.post(new HandlerRunnable(collectBean, queryListener));
            }
        });

    }
    public void queryDB(final String Xm, final String s ,final QueryListener queryListener){
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                //条件查询
                QueryBuilder<CollectBean> builder = new QueryBuilder<CollectBean>(CollectBean.class);
                builder.where(Xm,s);//加入条件

                ArrayList<CollectBean> collectBeen = mLiteOrm.query(builder);
                //放到主线程回调
                mHandler.post(new HandlerRunnable(collectBeen,queryListener));
            }
        });

    }

    public void unRepetition(final String a, final int i, final String s, final String s1, final QueryListener queryListener){
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                //条件查询
                QueryBuilder<CollectBean> builder = new QueryBuilder<CollectBean>(CollectBean.class);
                builder.where(a,i);//加入条件
                builder.whereAppendAnd();
                builder.whereAppend(s,s1);
                ArrayList<CollectBean> collectBeen = mLiteOrm.query(builder);
                //放到主线程回调
                mHandler.post(new HandlerRunnable(collectBeen,queryListener));
            }
        });
    }

    class HandlerRunnable implements Runnable {

        ArrayList<CollectBean> collectBeans;

        QueryListener mQueryListener;


        //构造方法
        public HandlerRunnable(ArrayList<CollectBean> collectBean, QueryListener queryListener) {
            collectBeans = collectBean;
            mQueryListener = queryListener;

        }

        @Override
        public void run() {
            mQueryListener.onQueryComplete(collectBeans);
        }
    }

    public interface QueryListener {
        void onQueryComplete(ArrayList<CollectBean> nativeBeen);


    }
}
